package com.yylending.las.lending.quota.entity;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yylending.las.personal.bandcard.Bank;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 选项卡工具类
 * Created by laixj on 2016/11/15.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */

public class OptionsUtil {
	private final static String TAG = "OptionsUtil";

	/**
	 * 根据label获取选项index
	 *
	 * @param options
	 * @param label
	 * @return
	 */
	public static int getOptionIndexByLabel(List<OptionBean> options, String label) {
		if (TextUtils.isEmpty(label) || null == options) {
			return -1;
		}
		int index = 0;
		int size = options.size();
		for (int i = 0; i < size; i++) {
			if (label.equals(options.get(i).itemName)) {
				index = i;
				break;
			}
		}
		return index;
	}

	/**
	 * 根据value获取选项index
	 *
	 * @param options
	 * @param value
	 * @return
	 */
	public static int getOptionIndexByValue(List<OptionBean> options, String value) {
		if (TextUtils.isEmpty(value) || null == options) {
			return -1;
		}
		int index = 0;
		int size = options.size();
		for (int i = 0; i < size; i++) {
			if (value.equals(options.get(i).itemNo)) {
				index = i;
				break;
			}
		}
		return index;
	}

	/**
	 * 根据label获取选项Bean
	 *
	 * @param options
	 * @param label
	 * @return
	 */
	public static OptionBean getOptionBeanByLabel(List<OptionBean> options, String label) {
		if (TextUtils.isEmpty(label) || null == options) {
			return null;
		}
		OptionBean optionBean = null;
		int size = options.size();
		for (int i = 0; i < size; i++) {
			if (label.equals(options.get(i).itemName)) {
				optionBean = options.get(i);
				break;
			}
		}
		return optionBean;
	}

	/**
	 * 根据value获取选项Bean
	 *
	 * @param options
	 * @param value
	 * @return
	 */
	public static OptionBean getOptionBeanByValue(List<OptionBean> options, String value) {
		if (TextUtils.isEmpty(value) || null == options) {
			return null;
		}
		OptionBean optionBean = null;
		int size = options.size();
		for (int i = 0; i < size; i++) {
			if (value.equals(options.get(i).itemNo)) {
				optionBean = options.get(i);
				break;
			}
		}
		return optionBean;
	}

	/**
	 * 读取json文件
	 *
	 * @param context
	 * @param fileName
	 * @return
	 */
	public static String getJsonStr(Context context, String fileName) {
		String resultString = "";
		InputStream inputStream;
		try {
			inputStream = context.getResources().getAssets().open(fileName);
			byte[] buffer = new byte[inputStream.available()];
			inputStream.read(buffer);
			resultString = new String(buffer, "utf-8");
			if (null != inputStream) {
				inputStream.close();
			}
		} catch (Exception e) {
		}
		return resultString;
	}

	/**
	 * 根据value获取选项index
	 *
	 * @param options
	 * @param value
	 * @return
	 */
	public static int getBankIndexByValue(List<Bank> options, String value) {
		if (TextUtils.isEmpty(value) || null == options) {
			return -1;
		}
		int index = 0;
		int size = options.size();
		for (int i = 0; i < size; i++) {
			if (value.equals(options.get(i).getValue())) {
				index = i;
				break;
			}
		}
		return index;
	}
}
